package components.tools
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_input;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import classes.c_events;
	
	import components.c_component;
	import components.c_pcomponent;
	import components.editor.c_baseElement;
	import components.editor.c_dragRect;
	import components.editor.c_editedElement;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class c_info extends c_pcomponent {
		public static const INFOWIDTH: int = 140;
		
		private var _pos		: uif_input;
		private var _size		: uif_input;
		private var _dragRect	: c_dragRect;
		
		public function c_info(parent:uif_component, styleClass:Object='') {
			super(parent, styleClass);
		}
		
		override protected function getPortRect(a_parent: uif_component=null): Rectangle {
			return a_parent.getInner(a_parent.width - INFOWIDTH, 0, 0, 0);
		}
		
		override protected function createComponents():void {
			var inner: Rectangle = getInner();
			var space: Number 	= getSpace(true);
			var swidth: Number 	= inner.width / 2 - space;
			_pos 	= new uif_input(this, inner.x, inner.y, swidth, inner.height, style.text);
			_size 	= new uif_input(this, _pos.x + _pos.width + space, inner.y, swidth, inner.height, style.text);
			_pos.addEvent(Event.CHANGE, doPosChange, _pos.textField);
			_size.addEvent(Event.CHANGE, doSizeChange, _size.textField);
		}
		
		private function doPosChange(e: Event): void {
			if (_dragRect && _dragRect.link) {
				var a_pos: Array = _pos.text.split(':');
				if ((a_pos.length > 1) && (a_pos[0] > 0) && (a_pos[1] > 0)) {
					_dragRect.startEditing();
					_dragRect.position = new Point(a_pos[0], a_pos[1]);
					_dragRect.endEditing();
				}
			}
		}
		
		private function doSizeChange(e: Event): void {
			if (_dragRect && _dragRect.link) {
				var a_size: Array = _size.text.split('x');
				if ((a_size.length > 1) && (a_size[0] > 0) && (a_size[1] > 0)) {
					_dragRect.startEditing();
					_dragRect.resize(a_size[0], a_size[1]);
					_dragRect.endEditing();
				}
			}
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(c_events.CHANGECURRENTELEMENT, doChangeCurrent, workspace);
		}
		
		private function doChangeCurrent(e: ObjectEvent): void {
			if (!_dragRect) {
				_dragRect = (e.object as c_editedElement).editor.dragRect;
				_dragRect.addEvent(uif_component.RESIZE, doResize);
				_dragRect.addEvent(uif_component.POSITION, doPosition);
			}
			updateInfo();
		}
		
		private function updateInfo(): void {
			_pos.text 	= Math.round(_dragRect.x) + ':'  + Math.round(_dragRect.y);
			_size.text 	= Math.round(_dragRect.width) + 'x'  + Math.round(_dragRect.height);
		}
		
		private function doResize(e: Event): void {
			updateInfo();
		}
		
		protected function doPosition(e: Event): void {
			updateInfo();
		}		
		
	}
}